class_name ScheduledEffect
extends Resource

enum TriggerType {
	TURN_COUNT,      # 基于回合数触发
	PHASE_SPECIFIC,  # 特定阶段触发
	RESOURCE_THRESHOLD, # 资源阈值触发
	CONDITIONAL      # 条件触发
}

enum TargetType {
	PLAYER,
	ENEMY,
	ALL_ENEMIES,
	SELF,
	CUSTOM
}

@export var id: String
@export var trigger_type: TriggerType
@export var target_type: TargetType
@export var trigger_turn: int = 0  # 触发回合数
@export var trigger_phase: int = 0  # Battle.TurnPhase enum value
@export var resource_type: String = ""  # 监控的资源类型 (health, mana, block等)
@export var resource_threshold: int = 0
@export var is_repeating: bool = false  # 是否重复触发
@export var repeat_interval: int = 1   # 重复间隔
@export var max_triggers: int = -1     # 最大触发次数，-1为无限
@export var condition_script: String = ""  # 自定义条件脚本

# 效果数据
@export var stat_modifications: Array = []  # Array[StatModification]
@export var status_effects: Array = []      # Array[Status]
@export var custom_effects: Array = []      # Array[Effect]

var current_triggers: int = 0
var last_trigger_turn: int = -1
var is_active: bool = true

func can_trigger(current_turn: int, current_phase: int, target: Node) -> bool:
	if not is_active:
		print("  ❌ 效果 %s 未激活" % id)
		return false
		
	if max_triggers > 0 and current_triggers >= max_triggers:
		print("  ❌ 效果 %s 已达到最大触发次数 (%d/%d)" % [id, current_triggers, max_triggers])
		return false
	
	match trigger_type:
		TriggerType.TURN_COUNT:
			if is_repeating:
				var can_trigger_repeat = current_turn >= trigger_turn and (current_turn - trigger_turn) % repeat_interval == 0
				print("  🔄 回合计数重复触发检查: %s (回合 %d, 目标回合 %d, 间隔 %d) = %s" % [id, current_turn, trigger_turn, repeat_interval, can_trigger_repeat])
				return can_trigger_repeat
			else:
				var can_trigger_once = current_turn == trigger_turn
				print("  🔄 回合计数单次触发检查: %s (当前回合 %d, 目标回合 %d) = %s" % [id, current_turn, trigger_turn, can_trigger_once])
				return can_trigger_once
		
		TriggerType.PHASE_SPECIFIC:
			# 检查阶段是否匹配
			if current_phase != trigger_phase:
				print("  ❌ 阶段不匹配: %s (当前阶段 %d, 目标阶段 %d)" % [id, current_phase, trigger_phase])
				return false
			
			# 如果设置了trigger_turn，检查回合数是否匹配
			if trigger_turn > 0:
				if current_turn != trigger_turn:
					print("  ❌ 回合不匹配: %s (当前回合 %d, 目标回合 %d)" % [id, current_turn, trigger_turn])
					return false
			
			# 如果是重复触发，检查间隔
			if is_repeating:
				# 对于重复触发的阶段特定效果，检查是否在正确的回合间隔
				if last_trigger_turn == -1:
					# 第一次触发
					print("  ✅ 阶段特定重复触发: %s 第一次触发" % id)
					return true
				else:
					# 检查是否满足重复间隔
					var can_trigger_repeat = (current_turn - last_trigger_turn) >= repeat_interval
					print("  🔄 阶段特定重复触发检查: %s (当前回合 %d, 上次触发回合 %d, 间隔 %d) = %s" % [id, current_turn, last_trigger_turn, repeat_interval, can_trigger_repeat])
					return can_trigger_repeat
			else:
				# 非重复触发，在阶段匹配时立即触发
				var can_trigger_once = true
				print("  🔄 阶段特定单次触发检查: %s (阶段匹配，立即触发) = %s" % [id, can_trigger_once])
				return can_trigger_once
		
		TriggerType.RESOURCE_THRESHOLD:
			var can_trigger_threshold = _check_resource_threshold(target)
			print("  🔄 资源阈值触发检查: %s = %s" % [id, can_trigger_threshold])
			return can_trigger_threshold
		
		TriggerType.CONDITIONAL:
			var can_trigger_conditional = _evaluate_condition(target)
			print("  🔄 条件触发检查: %s = %s" % [id, can_trigger_conditional])
			return can_trigger_conditional
	
	return false

func _check_resource_threshold(target: Node) -> bool:
	if not target or resource_type.is_empty():
		return false
	
	var stats = null
	if target.has_method("get_stats"):
		stats = target.get_stats()
	elif "stats" in target:
		stats = target.stats
	
	if not stats:
		return false
	
	match resource_type:
		"health":
			return stats.health <= resource_threshold
		"mana":
			if "mana" in stats:
				return stats.mana <= resource_threshold
			else:
				return false
		"block":
			return stats.block >= resource_threshold
		_:
			return false

func _evaluate_condition(target: Node) -> bool:
	# 简单的条件评估，可以扩展为更复杂的脚本系统
	if condition_script.is_empty():
		return false
	
	# 这里可以实现更复杂的条件评估逻辑
	return false

func trigger_effect(target: Node) -> void:
	current_triggers += 1
	
	# 获取当前回合数
	var current_turn = 0
	if target and target.get_tree():
		var battle = target.get_tree().get_first_node_in_group("battle")
		if battle:
			current_turn = battle.current_turn
	
	last_trigger_turn = current_turn
	print("🎯 效果 %s 触发，当前触发次数: %d, 上次触发回合: %d" % [id, current_triggers, last_trigger_turn])
	
	# 只有当目标不为null时才应用状态修改和状态效果
	if target:
		# 应用状态修改
		for stat_mod in stat_modifications:
			stat_mod.apply_to_target(target)
		
		# 应用状态效果
		for status in status_effects:
			if target.has_method("add_status"):
				target.add_status(status)
	
	# 应用自定义效果（无论目标是否为null）
	for effect in custom_effects:
		var targets: Array[Node] = []
		if target:
			targets.append(target)
		effect.execute(targets)
